package com.buzz.middleware.controller;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @Author: LiBaoxuan
 * @CreateTime: 2025-04-13  14:39
 * @Description: TODO
 */
@RestController
@RequestMapping("/resource")
public class ResourceController {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private RestTemplate restTemplate;

    private static final String RESOURCE_GET_URL = "http://localhost:8181/%s/resource/get?resourceType=%s";

    @GetMapping("/get")
    public String get(@RequestParam(required = true) String resourceType) {
        String contextPath = stringRedisTemplate.opsForValue().get(resourceType);
        if (contextPath == null) {
            return "未找到对应资源";
        }
        ResponseEntity<String> response = restTemplate.exchange(String.format(RESOURCE_GET_URL, contextPath, resourceType), HttpMethod.GET, null, String.class);
        return response.getBody();
    }

}
